#include "settingdialog.h"
#include "ui_settingdialog.h"
#include "settings.h"

#include <QFileDialog>
#include <QSettings>


SettingDialog::SettingDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SettingDialog)
{
    ui->setupUi(this);

    QSettings settings(QSettings::IniFormat, QSettings::UserScope, SETTING_ORGANIZATION, SETTING_APPLICATION);

    QString mplayerCmd = settings.value(INI_KEY_MPLAYER_COMMAND, DEFAULT_MPLAYER_COMMAND).toString();
    ui->mplayerCmdEdit->setText(mplayerCmd);

    QString videoDir = settings.value(INI_KEY_VIDEO_DIRECTORY, QDir::homePath()).toString();
    ui->videoDirEdit->setText(videoDir);
}

SettingDialog::~SettingDialog()
{
    delete ui;
}

void SettingDialog::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void SettingDialog::on_openVideoDIrButton_clicked()
{
    QString name = QFileDialog::getExistingDirectory(this, tr("Select directory for saving "), QDir::homePath());
    if (name != "")
        ui->videoDirEdit->setText(name);
}

void SettingDialog::on_buttonBox_accepted()
{
    QString videoDir = ui->videoDirEdit->text();
    QString mplayerCmd = ui->mplayerCmdEdit->text();

    QSettings settings(QSettings::IniFormat, QSettings::UserScope, SETTING_ORGANIZATION, SETTING_APPLICATION);
    settings.setValue(INI_KEY_VIDEO_DIRECTORY, videoDir);
    settings.setValue(INI_KEY_MPLAYER_COMMAND, mplayerCmd);
}
